package com.kandinfo.survey.patient.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kandinfo.base.security.domain.User;
import com.kandinfo.base.security.domain.UserInfo;
import com.kandinfo.base.security.repository.UserInfoRepository;
import com.kandinfo.base.security.repository.UserRepository;
import com.kandinfo.survey.common.dto.PatientDto;
import com.kandinfo.survey.common.dto.PatientSearchConditionDto;
import com.kandinfo.survey.patient.service.PatientManageService;

@Service
public class PatientManageServiceImpl implements PatientManageService {

    private static final Logger logger = LoggerFactory.getLogger(PatientManageServiceImpl.class);

    @Autowired
    UserInfoRepository userInfoRepository;
    @Autowired
    UserRepository userRepository;

    @Override
    public List<PatientDto> findByCondition(PatientSearchConditionDto conditionDto) {
        // TODO Auto-generated method stub
        // FIXME
        return null;
    }

    @Override
    public PatientDto findDto(Long patientId) {
        PatientDto responseDto = new PatientDto();

        User patient = userRepository.findOne(patientId);
        UserInfo patientInfo = userInfoRepository.findByUserId(patientId);
        if (patientInfo == null) {
            return null;
        }
        try {
            // first user
            BeanUtils.copyProperties(responseDto, patient);
            // then userInfo
            BeanUtils.copyProperties(responseDto, patientInfo);
        } catch (Exception e) {
            logger.error("对象转换错误！PatientServiceImpl - findOneDto ", e.toString());
            return null;
        }
        return responseDto;
    }

    @Override
    public List<PatientDto> findDtos(List<Long> idList) {
        List<PatientDto> resultList = new ArrayList<>();
        try {
            for (Long patientId : idList) {
                PatientDto dto = this.findDto(patientId);
                resultList.add(dto);
            }
        } catch (Exception e) {
            logger.error("对象转换错误！PatientServiceImpl - findDtos - findOneDto ", e.toString());
            return null;
        }

        return resultList;
    }

}
